package com.itheima.web;

import com.itheima.common.R;
import com.itheima.utile.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@RestController
public class CodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/code")
    public R code(String phone){

        String code = ValidateCodeUtils.generateValidateCode4String(4);

        ValueOperations opsForValue = redisTemplate.opsForValue();

        opsForValue.set(phone,code,1,TimeUnit.MINUTES);

        return R.success(code);

    }
}
